﻿using Decoration.IService;
using Decoration.Models;
using Decoration.Models.Dto;
using Decoration.Models.DTO;
using Decoration.Models.Models;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Decoration.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RoleMenuController : ControllerBase
    {
        // GET: api/<RoleMenuController>
        IRoleMenuService svc;
        IMenuInfoService menuInfoService;
        public RoleMenuController(IRoleMenuService svc, IMenuInfoService menuInfoService) 
        {
           this.svc = svc;    
            this.menuInfoService = menuInfoService;
        }


        [HttpGet]
        public PagedResult<Base_RoleMenu> Get(int pageIndex=1,int pageSize=10)
        {
            var data = svc.Query(o=>true);

            int total=data.Count();
            data = data.OrderByDescending(o => o.CreateTime).Skip(pageSize * (pageIndex - 1)).Take(pageSize);
            return new PagedResult<Base_RoleMenu>() {code=1,msg="成功",data=data, total = total};
        }

        // GET api/<RoleMenuController>/5
        [HttpGet("{id}")]
        public Result Get(int id)
        {
            var info = svc.Get(o => o.Id == id);
            if (info == null)
            {
                return new Result() { code = -1, msg = "为查询到数据" };
            }
            return new Result() { code = 1, msg = "成功", data = info };
        }

        // POST api/<RoleMenuController>
        [HttpPost]
        public Result Post(Base_RoleMenu rm)
        {
            var info = new Base_RoleMenu() 
            {
             CreateTime = DateTime.Now,
             MenuId = rm.MenuId,
             RoleId = rm.RoleId,
             Role = rm.Role,
             Status = rm.Status,
            }; 
            if (info == null)
            {
                return new Result() { code = -1, msg = "数据不能为空" };
            }
            var res =svc.Add(info);
            return new Result() { code = 1, msg = "成功",data=res};

        }

        // PUT api/<RoleMenuController>/5
        [HttpPut]
        public Result Put(Base_RoleMenu rm)
        {
            var info = svc.Get(o => o.Id == rm.Id);
            if (info == null)
            {
                return new Result() { code = -1, msg = "为查询到数据" };
            }
            info.Status = rm.Status;
            info.MenuId = rm.MenuId;
            info.RoleId = rm.RoleId;
            info.Role = rm.Role;
            var res=svc.Update(info);
            return new Result() { code = 1,msg="成功",data=res};

        }

        // DELETE api/<RoleMenuController>/5
        [HttpDelete("{id}")]
        public Result Delete(int id)
        {
            var info = svc.Get(o => o.Id == id);
            if (info == null)
            {
                return new Result() { code = -1, msg = "为查询到数据" };
            }
            info.Status = 0;
            var res = svc.Update(info);
            return new Result() { code = 1, msg = "成功", data = res };
        }

        
        [HttpGet]
        [Route("tree")]
        public Result GetTree()
        {
            var l = menuInfoService.Query(o => o.Status == 1 && o.IsShow == 1).ToList();
            var one = menuInfoService.Query(o => o.PId == 0 && o.Status == 1);
            List<ElementTree> list = new List<ElementTree>();
            //构造一级菜单
            foreach (var item in one)
            {
                var menu = new ElementTree() { id = item.Id, label = item.MenuName };
                //构建二级菜单
                var children = l.Where(o => o.PId == item.Id).ToList();
                var cs = new List<ElementTree>();
                foreach (var c in children)
                {
                    cs.Add(new ElementTree() { id = c.Id, label = c.MenuName });

                }
                menu.children = cs;
                if (cs.Count > 0)
                {
                    list.Add(menu);
                }
            }

            var result = new Result() { code = 1, msg = "成功", data = list };

            return result;

        }
        [HttpGet]
        [Route("rolemenu")]
        public Result GetMenus(int RoleId)
        {
            var query = svc.Query(o => o.RoleId == RoleId).ToList();
            return new Result() { code = 1, msg = "成功", data = query };
        }
        [HttpPost]
        [Route("batch")]
        public Result Batch(RoleMenusDto dto)
        {
            List<Base_RoleMenu> list = new List<Base_RoleMenu>();
            foreach (var menuId in dto.MenuIds)
            {
                list.Add(new Base_RoleMenu() { RoleId = dto.RoleId, MenuId = menuId, Status = 1, CreateTime = DateTime.Now });

            }
            var res = svc.Add(list);//删除的业务逻辑在service
            if (res)
            {
                return new Result() { code = 1, msg = "成功" };
            }
            else
            {
                return new Result() { code = -1, msg = "失败" };
            }

        }
    }
}
